package com.bbzn.user.client.service.impl;

import com.bbzn.user.client.dto.user.UserInfoDTO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.bbzn.user.client.dataobject.User;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.bbzn.user.client.dao.user.UserMapper;
import com.bbzn.user.client.service.UserService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(User record) {
        return userMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(User record) {
        return userMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public int insertBaseSelective(User record) {
        return userMapper.insertBaseSelective(record);
    }

    @Override
    public int selectCountBySelective(User record) {
        return userMapper.selectCountBySelective(record);
    }

    @Override
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int selectSubOneCount(Long pid) {
        return userMapper.selectSubOneCount(pid);
    }

    @Override
    public List<UserInfoDTO> selectByPid(Long pid) {
        List<UserInfoDTO> users = userMapper.selectByPid(pid);
        return getK(users);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public List<User> findByAll(User user) {
        return userMapper.findByAll(user);
    }

    @Override
    public int updateBatch(List<User> list) {
        return userMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<User> list) {
        return userMapper.batchInsert(list);
    }

    @Override
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public List<User> selectBatchIds(@Param("list") List<Long> ids) {
        return userMapper.selectBatchIds(ids);
    }

    @Override
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public List<Long> selectSubIdByPid(Long pid) {
        return userMapper.selectSubIdByPid(pid);
    }

    @Override
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public List<Long> selectPidById(Long id) {
        return userMapper.selectPidById(id);
    }

    @Override
    public int updateBatchSelective(List<User> list) {
        return userMapper.updateBatchSelective(list);
    }

    @Override
    public User selectByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }

    /**
     * 所有资源,按顺序显示
     * 显示出树状
     *
     * @return
     */
    public List<UserInfoDTO> getK(List<UserInfoDTO> users) {
        //JDK8的stream处理,把根分类区分出来
        List<UserInfoDTO> roots = users.stream().filter(dealCategory -> (dealCategory.getLevel() == 1)).collect(Collectors.toList());
        //对根分类进行排序
        roots.sort(new Comparator<UserInfoDTO>() {
            @Override
            public int compare(UserInfoDTO o1, UserInfoDTO o2) {
                return o1.getId() > o2.getId() ? 1 : -1;
            }
        });

        //把非根分类区分出来
        List<UserInfoDTO> subs = users.stream().filter(dealCategory -> (dealCategory.getLevel() != 1)).collect(Collectors.toList());

        //递归构建结构化的分类信息
        roots.forEach(root -> buildSubs(root, subs));
        return roots;
    }

    /**
     * 递归构建
     *
     * @param parent
     * @param subs
     */
    private void buildSubs(UserInfoDTO parent, List<UserInfoDTO> subs) {
        List<UserInfoDTO> children = subs.stream().filter(sub -> (sub.getPid().equals(parent.getId()))).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(children)) {//有子分类的情况
            parent.setChildren(children);
            children.forEach(child -> buildSubs(child, subs));//再次递归构建
        }
    }
}










